//
//  FPRegisAndForgetView.m
//  ForestPack
//
//  Created by jimmy on 2018/7/3.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "FPRegisAndForgetView.h"
#import "Masonry.h"

@interface FPRegisAndForgetView()<UITextFieldDelegate>

//{
//    UITextField   * phoneNumTextField;
//    UITextField   * messageTextField;
//    UITextField   * passwordTextField;
//    UITextField   * confirmPasswordTextField;
//    UIButton      * getVerifycodeBtn;
//    
//}

@property (nonatomic,strong) NSTimer   *verCodeTimer;//验证码计时器
@property (nonatomic,assign) NSInteger verCodeTime;
@property (nonatomic,strong) UIButton  *verCodeBtn;//发送验证码按钮


@end

@implementation FPRegisAndForgetView

- (void)getVerifyCode
{
    NSString *url = @"";
    NSDictionary *parameter = @{};
    UITextField *phoneField = [self viewWithTag:200];
    if ([phoneField.text isEqualToString:@""]) {
        [HttpManager showNoteMsg:@"请输入手机号"];
        return;
    }
    if (self.isMsgRegist == NO) {
        [self getVerticodeNum];
    }else{
        url = [NSString stringWithFormat:@"%@app/mobile",REQUESTHEADER];//先验证一下手机号是否可以注册，可以的话，再能发送验证码
        parameter = @{@"mobile":phoneField.text};
        [HttpManager postHttpRequestByPost:url andParameter:parameter success:^(id successResponse) {
            NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
            if ([status isEqualToString:@"1"]) {
                
                [self getVerticodeNum];
                
            }else{
                [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
            }
        } andFailure:^(id failureResponse) {
            [HttpManager showFail];
        }];
    }
}

-(void)getVerticodeNum
{
    NSDictionary *paramDict = @{};
    UITextField *phoneField = [self viewWithTag:200];
    if (self.isMsgRegist == NO) {
        paramDict = @{@"mobile":phoneField.text,@"type":@"4"};
    }else{
        paramDict = @{@"mobile":phoneField.text,@"type":@"1"};
    }
    
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/mobile_send_code",REQUESTHEADER] andParameter:paramDict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
            
            //                    这里用block传点击事件
            //                    if (self->_nextClick) {
            //                        self->_nextClick();
            //                    }
            self.verCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCountAction) userInfo:nil repeats:YES];
            self.verCodeTime = 60;
            [self.verCodeTimer fire];
            self.verCodeBtn.backgroundColor = UIColorFromRGB(@"#d1d0d0", 1);
            self.verCodeBtn.enabled = NO;
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    }andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)timerCountAction{
    self.verCodeTime--;
    
    self.verCodeBtn.enabled = NO;
    [self.verCodeBtn setTitle:[NSString stringWithFormat:@"%ld秒后重发",(long)self.verCodeTime] forState:UIControlStateNormal];
    if (self.verCodeTime == 0) {
        self.verCodeTime = 60;
        self.verCodeBtn.backgroundColor = ThemeColor;
        self.verCodeBtn.enabled = YES;
        [self.verCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.verCodeTimer invalidate];
        self.verCodeTimer = nil;
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
//    _isMsgRegist = NO;
    
    UIView* inputView = [[UIView alloc] init];
    inputView.backgroundColor = [UIColor clearColor];
    [self addSubview:inputView];
    [inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self);
        make.width.equalTo(self.mas_width);
        make.height.equalTo(@(ScreenX375(236)));
    }];
    UIImageView* phoneImgView = [[UIImageView alloc] init];
    phoneImgView.image = [UIImage imageNamed:@"手机号"];
    [inputView addSubview:phoneImgView];
    [phoneImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(inputView.mas_left).offset(ScreenX375(40));
        make.top.equalTo(inputView.mas_top).offset(ScreenX375(26));
        make.size.mas_equalTo(CGSizeMake(ScreenX375(16),ScreenX375(22)));
    }];
    
    self.phoneNumTextField = [[UITextField alloc] init];
    self.phoneNumTextField.tag = 200;
    self.phoneNumTextField.delegate = self;
    self.phoneNumTextField.font = [UIFont systemFontOfSize:14];
    self.phoneNumTextField.placeholder = @"请输入您注册的手机号";
    self.phoneNumTextField.returnKeyType = UIReturnKeyDone;
    self.phoneNumTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneNumTextField.textAlignment = NSTextAlignmentLeft;
    self.phoneNumTextField.clearButtonMode =  UITextFieldViewModeWhileEditing;
    [inputView addSubview:self.phoneNumTextField];
    [self.phoneNumTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneImgView.mas_right).offset(ScreenX375(16));
        make.right.equalTo(inputView.mas_right).offset(-ScreenX375(16));
        make.top.equalTo(inputView.mas_top).offset(ScreenX375(26));
        make.height.equalTo(@(ScreenX375(20)));
    }];
    UIImageView* line1 = [[UIImageView alloc] init];
    line1.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    [inputView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(inputView).offset(ScreenX375(40));
        make.right.equalTo(inputView).offset(-ScreenX375(40));
        make.top.equalTo(inputView.mas_top).offset(ScreenX375(60));
        make.height.equalTo(@0.5);
    }];
    //codeinput
    UIImageView* codeImgView = [[UIImageView alloc] init];
    codeImgView.image = [UIImage imageNamed:@"验证码"];
    [inputView addSubview:codeImgView];
    [codeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(inputView.mas_left).offset(ScreenX375(40));
        make.top.equalTo(line1.mas_bottom).offset(ScreenX375(26));
        make.size.mas_equalTo(CGSizeMake(ScreenX375(16),ScreenX375(22)));
    }];
    self.verCodeBtn = [[UIButton alloc] init];
    self.verCodeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    self.verCodeBtn.layer.cornerRadius = ScreenX375(15);
    self.verCodeBtn.layer.masksToBounds = YES;
    self.verCodeBtn.layer.borderWidth = 1.0;
    self.verCodeBtn.layer.borderColor = [UIColor clearColor].CGColor;
    self.verCodeBtn.backgroundColor = RGB_COLOR(237, 34, 59);
    [self.verCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.verCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [self.verCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
//    [self.verCodeBtn setBackgroundColor:UIColorFromRGB(@"#ffa700", 0.1) forState:UIControlStateNormal];
//    [self.verCodeBtn setBackgroundColor:UIColorFromRGB(@"#d1d0d0", 1) forState:UIControlStateDisabled];
    [self.verCodeBtn addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
    [inputView addSubview:self.verCodeBtn];
    self.verCodeBtn.enabled = YES;
    [self.verCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(inputView.mas_right).offset(-ScreenX375(40));
        make.top.equalTo(line1.mas_bottom).offset(ScreenX375(20));
        make.width.equalTo(@(ScreenX375(84)));
        make.height.equalTo(@(ScreenX375(30)));
        
    }];
    self.messageTextField = [[UITextField alloc] init];
    self.messageTextField.tag = 1002;
    self.messageTextField.delegate = self;
    self.messageTextField.font = [UIFont systemFontOfSize:14];
    self.messageTextField.placeholder = @"请输入您的验证码";
    self.messageTextField.returnKeyType = UIReturnKeyDone;
    self.messageTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.messageTextField.textAlignment = NSTextAlignmentLeft;
    self.messageTextField.clearButtonMode =  UITextFieldViewModeWhileEditing;
    [inputView addSubview:self.messageTextField];
    [self.messageTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(codeImgView.mas_right).offset(ScreenX375(16));
        make.right.equalTo(self.mas_right).offset(-ScreenX375(125));
        make.top.equalTo(line1.mas_bottom).offset(ScreenX375(16));
        make.height.equalTo(@(ScreenX375(40)));
    }];
    UIImageView* line2 = [[UIImageView alloc] init];
    line2.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    [inputView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(inputView).offset(ScreenX375(40));
        make.right.equalTo(inputView).offset(-ScreenX375(40));
        make.top.equalTo(inputView.mas_top).offset(ScreenX375(120));
        make.height.equalTo(@0.5);
    }];
    UIImageView* codeImgView1 = [[UIImageView alloc] init];
    codeImgView1.image = [UIImage imageNamed:@"密码"];
    [inputView addSubview:codeImgView1];
    [codeImgView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(inputView.mas_left).offset(ScreenX375(40));
        make.top.equalTo(line2.mas_bottom).offset(ScreenX375(26));
        make.size.mas_equalTo(CGSizeMake(ScreenX375(16),ScreenX375(22)));
    }];
    self.passwordTextField = [[UITextField alloc] init];
    self.passwordTextField.tag = 1003;
    self.passwordTextField.delegate = self;
    self.passwordTextField.font = [UIFont systemFontOfSize:14];
    self.passwordTextField.placeholder = @"请输入新密码";
    self.passwordTextField.returnKeyType = UIReturnKeyDone;
    self.passwordTextField.keyboardType = UIKeyboardTypeDefault;
    self.passwordTextField.textAlignment = NSTextAlignmentLeft;
    self.passwordTextField.secureTextEntry = YES;
    self.passwordTextField.clearButtonMode =  UITextFieldViewModeWhileEditing;
    [inputView addSubview:self.passwordTextField];
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(codeImgView1.mas_right).offset(ScreenX375(16));
        make.right.equalTo(inputView.mas_right).offset(-ScreenX375(40));
        make.top.equalTo(line2.mas_bottom).offset(ScreenX375(16));
        make.height.equalTo(@(ScreenX375(40)));
    }];
    UIImageView* line3 = [[UIImageView alloc] init];
    line3.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    [inputView addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(inputView).offset(ScreenX375(40));
        make.right.equalTo(inputView).offset(-ScreenX375(40));
        make.top.equalTo(inputView.mas_top).offset(ScreenX375(180));
        make.height.equalTo(@0.5);
    }];
    UIImageView* codeImgView2 = [[UIImageView alloc] init];
    codeImgView2.image = [UIImage imageNamed:@"密码"];
    [inputView addSubview:codeImgView2];
    [codeImgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(inputView.mas_left).offset(ScreenX375(40));
        make.top.equalTo(line3.mas_bottom).offset(ScreenX375(26));
        make.size.mas_equalTo(CGSizeMake(ScreenX375(16),ScreenX375(22)));
    }];
    self.confirmPasswordTextField = [[UITextField alloc] init];
    self.confirmPasswordTextField.tag = 1004;
    self.confirmPasswordTextField.delegate = self;
    self.confirmPasswordTextField.font = [UIFont systemFontOfSize:14];
    self.confirmPasswordTextField.placeholder = @"请再次输入新密码";
    self.confirmPasswordTextField.returnKeyType = UIReturnKeyDone;
    self.confirmPasswordTextField.keyboardType = UIKeyboardTypeDefault;
    self.confirmPasswordTextField.textAlignment = NSTextAlignmentLeft;
    self.confirmPasswordTextField.secureTextEntry = YES;
    self.confirmPasswordTextField.clearButtonMode =  UITextFieldViewModeWhileEditing;
    [inputView addSubview:self.confirmPasswordTextField];
    [self.confirmPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(codeImgView2.mas_right).offset(ScreenX375(16));
        make.right.equalTo(inputView.mas_right).offset(-ScreenX375(40));
        make.top.equalTo(line3.mas_bottom).offset(ScreenX375(16));
        make.height.equalTo(@(ScreenX375(40)));
    }];
    UIImageView* line4 = [[UIImageView alloc] init];
    line4.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    [inputView addSubview:line4];
    [line4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(inputView).offset(ScreenX375(40));
        make.right.equalTo(inputView).offset(-ScreenX375(40));
        make.bottom.equalTo(inputView.mas_bottom).offset(1);
        make.height.equalTo(@0.5);
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:true];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self resignFirstResponder];
}

@end
